﻿using System.Net.Http.Json;
using Aurora.AI.Abstractions;
using Aurora.AI.Abstractions.Chats;
using Aurora.AI.Abstractions.Extensions;
using Aurora.AI.Abstractions.ObjectModels.RequestModels;
using Aurora.AI.Abstractions.ObjectModels.ResponseModels;

namespace Aurora.AI.OpenAI.Chats;

public sealed class OpenAICompletionService(IHttpClientFactory httpClientFactory) : IAuroraAICompletionsService
{
    public async Task<CompletionCreateResponse> CompletionAsync(AuroraAICompletionCreateRequest createCompletionModel,
        AuroraAIPlatformOptions? options = null,
        CancellationToken cancellationToken = default)
    {
        var client = httpClientFactory.CreateClient(OpenAIPlatformOptions.PlatformCode);

        var response = await client.PostJsonAsync(options?.Address.TrimEnd('/') + "/v1/chat/completions",
            createCompletionModel, options.ApiKey);

        var result = await response.Content.ReadFromJsonAsync<CompletionCreateResponse>(
            cancellationToken: cancellationToken).ConfigureAwait(false);

        return result;
    }
}